/*
 * Flight.java
 *
 * Version:
 *  $Id: Flight.java 2 2011-03-29 07:21:11Z jhaberstro $
 *
 *
 * Revisions:
 *  $Rev: 2 $
 *
 */

import java.util.Scanner;

/**
 * The main Flight class responsible for main loop that controls the program
 * @author jhaberstro
 */
public class Flight
{
    /**
     * 
     * @param args
     */
    public static void main(String args[]) {
        PriorityQueue< Passenger > pq = new LinkedQueue< Passenger >();
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("Enter an option\n1 to Add a passenger to the queue\n2 to Remove and print the first passenger\n3 to quit");
            System.out.printf("Your choice: ");
            int option = -1;
            while(true) {
                if (!scanner.hasNextInt()) {
                    System.out.println("Please enter a valid integer option: ");
                    scanner.next();
                }
                else {
                    option = scanner.nextInt();
                    if (option >= 1 && option <= 3) {
                        break;
                    }
                }
            }
            
            switch(option) {
                case 1: {
                    // read name
                    System.out.printf("Passenger name: ");
                    scanner.nextLine(); // read the left over newline character
                    String name = scanner.nextLine();
                    
                    // read group
                    System.out.printf("Boarding Group: ");
                    String group = "D";
                    while (true) {
                        group = scanner.next();
                        if (group.equals("A") || group.equals("B") || group.equals("C")) {
                            break;
                        }
                        else {
                            System.out.printf("Please enter a valid group: ");
                        }
                    }
                    
                    // read sequence num
                    System.out.printf("Sequence: ");
                    int sequenceNum = 0;
                    while (true) {
                        sequenceNum = scanner.nextInt();
                        if (sequenceNum >= 1 && sequenceNum <= 60) {
                            break;
                        }
                        else {
                            System.out.printf("Please enter a sequence: ");
                        }
                    }
                    
                    pq.enqueue(new Passenger(name, group, sequenceNum));
                    break;
                }
                
                case 2: {
                    if (pq.isEmpty()) {
                        System.out.println("Queue is empty.");
                    }
                    else {
                        Passenger p = pq.dequeue();
                        System.out.println("Removing: " + p.getName() + " in seat " + p.getGroup() + p.getSequence());                        
                    }
                    
                    break;
                }
                
                case 3: {
                    return;
                }
            }
        }
    }
}